package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiT_user_cash_records;
import cn.com.api.entity.ApiU_message;
import cn.com.api.mapper.ApiT_user_cash_recordsMapper;
import cn.com.api.mapper.ApiU_messageMapper;
import cn.com.api.service.ApiT_user_cash_recordsService;
import cn.com.api.service.impl.base.T_user_cash_recordsServiceImplBase;
import cn.com.util.DateUtil;

/**
 * 
 **/
@Service
public class ApiT_user_cash_recordsServiceImpl extends T_user_cash_recordsServiceImplBase
		implements ApiT_user_cash_recordsService {

	@Autowired
	private ApiT_user_cash_recordsMapper apiT_user_cash_recordsMapper;
	@Autowired
	private ApiU_messageMapper apiU_messageMapper;

	// 新增对象后返回记录ID
	public int T_user_cash_recordsInsertion(ApiT_user_cash_records t_user_cash_records) {
		return this.apiT_user_cash_recordsMapper.T_user_cash_recordsInsertion(t_user_cash_records);
	}

	// 获取用户金额明细
	public List<ApiT_user_cash_records> getUserCashRecords(String user_id, String date) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		map.put("date", date);
		return apiT_user_cash_recordsMapper.findUserCashRecords(map);
	}

	// 回收未领取的转账
	@Transactional
	public void takeBackTransfer(Integer hours) throws Exception {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("hours", hours);
		String nowTimeToString = DateUtil.nowTimeToString();
		List<ApiT_user_cash_records> list = apiT_user_cash_recordsMapper.findTransferCashRecords(map);
		List<ApiU_message> messages = new ArrayList<ApiU_message>();
		for (ApiT_user_cash_records records : list) {
			records.setIs_receive(2);
			apiT_user_cash_recordsMapper.updateT_user_cash_records(records);
			// 添加消息
			ApiU_message message = new ApiU_message();
			message.setAdd_time(nowTimeToString);
			message.setConn_id(records.getRecord_id());
			message.setTitle("转账消息");
			message.setType(10);
			message.setUser_id(records.getUser_id());
			message.setIs_read(0);
			message.setContent("您的转账已退款");
			messages.add(message);
		}
		apiU_messageMapper.insertmessageList(messages);
	}

}
